﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using WindowsTheme.Helper;
using WindowsTheme.ThemeHelper;

namespace WindowsTheme
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
            InitializeComponent();
            InitData();
        }

        private List<string> ImgList = new List<string>();
        private void InitData()
        {
            FileHelper.Folder_existence(StaticValue.TempPath, true);
            FileHelper.Folder_existence(StaticValue.Win7ForBIN1, true);
            label3.BackColor = Color.FromArgb(StaticValue.Transparency, StaticValue.ColorR, StaticValue.ColorG, StaticValue.ColorB);
            label4.Text = string.Format("{0}秒自动切换",StaticValue.Interval);
        }

        private void button1_Click(object sender, EventArgs e)
        {
         
        }

        private void button2_Click(object sender, EventArgs e)
        {

            InitThemePath();
            FileSelect();
          
         
        }

        private  void FileSelect()
        {
            OpenFileDialog ThemeDialog = new OpenFileDialog();
            ThemeDialog.Multiselect = true;//该值确定是否可以选择多个文件
            ThemeDialog.Title = "请选择文件夹";
            ThemeDialog.Filter = "图像文件(*.jpg;*.jpeg;*.png)|*.jpg;*.jpeg;*.png";
            if (ThemeDialog.ShowDialog() == DialogResult.OK)
            {
                foreach (string FileName in ThemeDialog.FileNames)
                {
                 

                    String TipText = FileHelper.GetImgInfo(FileName);
                    if (TipText != "") {

                        Console.WriteLine(TipText);
                        new ImgAlert(TipText,FileName).ShowDialog();
                        if (StaticValue.JumpFlag1) {
                            StaticValue.JumpFlag1 = false;
                            continue;
                        }
                         
                    }

                    string JpgFileName = "";//需要判断下第一张图是否是 JPG结尾，如果是PNG结尾，这个主题无法安装
                    if ((Directory.GetFiles(StaticValue.ThemeImgFloder).Count() + 1) == 1)
                        JpgFileName = StaticValue.ThemeImgFloder + "Acg12ThemeCreteaHelper" + (Directory.GetFiles(StaticValue.ThemeImgFloder).Count() + 1) + ".jpg";
                    else
                        JpgFileName = StaticValue.ThemeImgFloder + "Acg12ThemeCreteaHelper" + (Directory.GetFiles(StaticValue.ThemeImgFloder).Count() + 1) + Path.GetExtension(FileName); 
                                      File.Copy(FileName, JpgFileName, true);
                    label2.Text = string.Format("当前已处理{0},总计{1}", ThemeDialog.FileNames.Count(), Directory.GetFiles(StaticValue.ThemeImgFloder).Count());
                }
            }
            ThemeDialog.Dispose();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            InitThemePath();
            
            String Type = "\"{0}ThemeCreateHelper.exe\" -r -p -P {1} N \"{2}\"  \"{3}\" ";
            Console.WriteLine(CreateCab.RunCode(string.Format(Type, StaticValue.BinPath//放置压缩theme.exe的路径
                , StaticValue.ThemeFloder//需要完全匹配的路径
                , "\"" + StaticValue.Win7SucessTheme + TextHelper.ReplaceBadChar(textBox1.Text) + ".themepack\""//处理好的theme文件
                , StaticValue.ThemeFloder + "*"//需要压缩的文件路径
                )));
            FileHelper.OpenFile(StaticValue.Win7SucessTheme);
        }

        private void InitThemePath()
        {
            ThemeNameHelper();
            FileHelper.Folder_existence(StaticValue.ThemeFloder, true);//创建主题文件夹，防止空指针
            Win7Create.ThemeCreate(StaticValue.ThemeFloder, TextHelper.ReplaceBadChar(textBox1.Text));
        }

        private void button4_Click(object sender, EventArgs e)
        {
            FileHelper.OpenFile(StaticValue.Win7SucessTheme);
        }

        private void button5_Click(object sender, EventArgs e)
        {
          
        }

       
        /// <summary>
        /// 创建主题名字对应的文件夹的辅助类
        /// </summary>
        /// <returns></returns>
        private bool ThemeNameHelper()
        {
            if (textBox1.Text != "")
            {
                String ThemeName = TextHelper.ReplaceBadChar(textBox1.Text);
                StaticValue.ThemeFloder = StaticValue.ThemePath + ThemeName + "\\";
                StaticValue.ThemeImgFloder = StaticValue.ThemeFloder + "DesktopBackground\\";
                if (StaticValue.ThemeFloder != "")
                {

                    FileHelper.Folder_existence(StaticValue.ThemeImgFloder, true);

                    return true;
                }
                

            }
            else
            {
                MessageBox.Show("请输入正确的主题名，以便创建对应路径！");
            }
            return false;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            InitThemePath();
            new SelectIcon(StaticValue.ThemeFloder, TextHelper.ReplaceBadChar(textBox1.Text)).ShowDialog();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (ThemeNameHelper())
            {
                Win7Create.ThemeCreate(StaticValue.ThemeFloder, TextHelper.ReplaceBadChar(textBox1.Text));
                colorDialog1.ShowDialog();
                StaticValue.ColorR = colorDialog1.Color.R;
                StaticValue.ColorG = colorDialog1.Color.G;
                StaticValue.ColorB = colorDialog1.Color.B;
                new ColorPrompt().ShowDialog();

                label3.BackColor = Color.FromArgb(StaticValue.Transparency, StaticValue.ColorR, StaticValue.ColorG, StaticValue.ColorB);

                String ThemeColor = string.Format("0X{0}{1}",
                    Convert.ToString(StaticValue.Transparency, 16).ToUpper()//透明度转码。
                    , ColorTranslator.ToHtml(Color.FromArgb(StaticValue.ColorR, StaticValue.ColorG, StaticValue.ColorB)).Replace("#", "")
                   );
                //ColorizationColor=0X[\w\W]{8}
                Console.WriteLine(ThemeColor);
                FileHelper.TextFileReplace(StaticValue.ThemeFloder + TextHelper.ReplaceBadChar(textBox1.Text) + ".theme", @"ColorizationColor=0X[\w\W]{8}", "ColorizationColor=" + ThemeColor);
            }
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)
        {
            InitThemePath();
            new SwitchTime(StaticValue.ThemeFloder + TextHelper.ReplaceBadChar(textBox1.Text) + ".theme").ShowDialog();
            label4.Text = string.Format("{0}秒自动切换", StaticValue.Interval);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            new Setting().ShowDialog();
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            InitThemePath();
            new Copyright().ShowDialog();
           

         
        }

        private void button9_Click(object sender, EventArgs e)
        {
         new ThemeCreate().ShowDialog();
           
        }

      
    }
}
